<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用给定参数加密明文</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.mcrypt-encrypt" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mcrypt_encrypt</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.2, PHP 5, PHP 7 &lt; 7.2.0, PECL mcrypt &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">mcrypt_encrypt</span> &mdash; <span class="dc-title">使用给定参数加密明文</span></p>

 </div>
 
 <div id="function.mcrypt-encrypt-refsynopsisdiv">
  <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function has been
<em class="emphasis">DEPRECATED</em> as of PHP 7.1.0. Relying on this function
is highly discouraged.</p></div>
 </div>
 
 <div class="refsect1 description" id="refsect1-function.mcrypt-encrypt-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mcrypt_encrypt</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$cipher</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$key</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$data</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$mode</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$iv</code></span>
  ] ) : <span class="type">string</span></div>

  <p class="para rdfs-comment">
   加密数据并返回密文。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mcrypt-encrypt-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">cipher</code></dt>

     <dd>

      <p class="para"><strong><code>MCRYPT_ciphername</code></strong> 常量中的一个，或者是字符串值的算法名称。</p>
     </dd>

    
    
     <dt>
<code class="parameter">key</code></dt>

     <dd>

      <p class="para">
       加密密钥。
       如果密钥长度不是该算法所能够支持的有效长度，则函数将会发出警告并返回 <strong><code>FALSE</code></strong>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">data</code></dt>

     <dd>

      <p class="para">
       使用给定的 <code class="parameter">cipher</code> 和 <code class="parameter">mode</code>
       加密的数据。
       如果数据长度不是 n*分组大小，则在其后使用 &#039;<em>\0</em>&#039; 补齐。
      </p>
      <p class="para">
       返回的密文长度可能比 <code class="parameter">data</code> 
       更大。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">mode</code></dt>

     <dd>

      <p class="para"><strong><code>MCRYPT_MODE_modename</code></strong> 常量中的一个，或以下字符串中的一个：&quot;ecb&quot;，&quot;cbc&quot;，&quot;cfb&quot;，&quot;ofb&quot;，&quot;nofb&quot; 和 &quot;stream&quot;。</p>
     </dd>

    
    
     <dt>
<code class="parameter">iv</code></dt>

     <dd>

      <p class="para">Used for the initialization in CBC, CFB, OFB modes, and in some algorithms in STREAM mode. If the provided IV size is not supported by the chaining mode or no IV was provided, but the chaining mode requires one, the function will emit a warning and return <strong><code>FALSE</code></strong>.</p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mcrypt-encrypt-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   以字符串方式返回密文， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

<div class="refsect1 changelog" id="refsect1-function.mcrypt-encrypt-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.6.0</td>
       <td>
        不再接受无效长度的 <code class="parameter">key</code> and <code class="parameter">iv</code> 参数。
        如果参数长度无效，则 <span class="function"><a href="mcrypt_decrypt.html" class="function">mcrypt_decrypt()</a></span> 函数会产生警告并且返回 <strong><code>FALSE</code></strong>。
        之前版本中，对于长度不足的密钥和初始向量会在其后补齐 &#039;<em>\0</em>&#039; 
        使其达到有效长度。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.mcrypt-encrypt-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="hash-hmac-algos.example.basic">
    <p><strong>Example #1 <span class="function"><strong>mcrypt_encrypt()</strong></span> 例程</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;---&nbsp;加密&nbsp;---<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;密钥应该是随机的二进制数据，<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;开始使用&nbsp;scrypt,&nbsp;bcrypt&nbsp;或&nbsp;PBKDF2&nbsp;将一个字符串转换成一个密钥<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;密钥是&nbsp;16&nbsp;进制字符串格式<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pack</span><span style="color: #007700">(</span><span style="color: #DD0000">'H*'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;显示&nbsp;AES-128,&nbsp;192,&nbsp;256&nbsp;对应的密钥长度：<br />&nbsp;&nbsp;&nbsp;&nbsp;#16，24，32&nbsp;字节。<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key_size&nbsp;</span><span style="color: #007700">=&nbsp;&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Key&nbsp;size:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$key_size&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$plaintext&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"This&nbsp;string&nbsp;was&nbsp;AES-256&nbsp;/&nbsp;CBC&nbsp;/&nbsp;ZeroBytePadding&nbsp;encrypted."</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;为&nbsp;CBC&nbsp;模式创建随机的初始向量<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iv_size&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_get_iv_size</span><span style="color: #007700">(</span><span style="color: #0000BB">MCRYPT_RIJNDAEL_128</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MCRYPT_MODE_CBC</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_create_iv</span><span style="color: #007700">(</span><span style="color: #0000BB">$iv_size</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MCRYPT_RAND</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;创建和&nbsp;AES&nbsp;兼容的密文（Rijndael&nbsp;分组大小&nbsp;=&nbsp;128）<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;仅适用于编码后的输入不是以&nbsp;00h&nbsp;结尾的<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;（因为默认是使用&nbsp;0&nbsp;来补齐数据）<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_encrypt</span><span style="color: #007700">(</span><span style="color: #0000BB">MCRYPT_RIJNDAEL_128</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$plaintext</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MCRYPT_MODE_CBC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;将初始向量附加在密文之后，以供解密时使用<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$iv&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$ciphertext</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;对密文进行&nbsp;base64&nbsp;编码<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext_base64&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">base64_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$ciphertext</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext_base64&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;===&nbsp;警告&nbsp;===<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;密文并未进行完整性和可信度保护，<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;所以可能遭受&nbsp;Padding&nbsp;Oracle&nbsp;攻击。<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;---&nbsp;解密&nbsp;---<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext_dec&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">base64_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$ciphertext_base64</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;初始向量大小，可以通过&nbsp;mcrypt_get_iv_size()&nbsp;来获得<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iv_dec&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$ciphertext_dec</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iv_size</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;获取除初始向量外的密文<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext_dec&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$ciphertext_dec</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iv_size</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;可能需要从明文末尾移除&nbsp;0<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$plaintext_dec&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_decrypt</span><span style="color: #007700">(</span><span style="color: #0000BB">MCRYPT_RIJNDAEL_128</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext_dec</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MCRYPT_MODE_CBC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iv_dec</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&nbsp;</span><span style="color: #0000BB">$plaintext_dec&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Key size: 32
ENJW8mS2KaJoNB5E5CoSAAu0xARgsR1bdzFWpEn+poYw45q+73az5kYi4j+0haevext1dGrcW8Qi59txfCBV8BBj3bzRP3dFCp3CPQSJ8eU=
This string was AES-256 / CBC / ZeroBytePadding encrypted.
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.mcrypt-encrypt-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="mcrypt_decrypt.html" class="function" rel="rdfs-seeAlso">mcrypt_decrypt()</a> - 使用给定参数解密密文</span></li>
    <li class="member"><span class="function"><a href="mcrypt_module_open.html" class="function" rel="rdfs-seeAlso">mcrypt_module_open()</a> - 打开算法和模式对应的模块</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>